package com.qdxwx.manage.controller.document;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.company.CompanyService;
import com.qdxwx.data.service.document.DocumentService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.document.Document;
import com.qdxwx.models.document.DocumentDetail;
import com.qdxwx.models.document.DocumentQuery;
import com.qdxwx.models.enums.DocumentType;
import com.qdxwx.models.member.MemberType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 文库 Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/document")
public class DocumentController extends BaseController {

    @Autowired
    private DocumentService documentService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private MemberTypeService memberTypeService;

    @Permissions("document:list")
    @GetMapping("/list.html")
    public String list(ModelMap mmap) {
        mmap.put("types",DocumentType.values());
        return "document/document-list";
    }

    @Permissions("document:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        Integer typeId = TKConvert.toInteger(request.getParameter("typeId"),null);
        String search = TKString.trim(request.getParameter("search"));
        if (TKConvert.toInteger(search) > 0) {
            Document document = documentService.get(TKConvert.toInteger(search));
            if (document == null) return TableDataInfo.error("找不到指定id的课程");
            List<Document> list = new ArrayList<>();
            list.add(document);
            return TableDataInfo.success(list, 1);
        }
        Integer status = TKConvert.toInteger(request.getParameter("status"),null);
        DocumentQuery query = new DocumentQuery();
        query.setChannelId(11);
        query.setTitle(TKString.isNotEmpty(search) ? search : null);
        query.setStatus(status);
        query.setTypeId(typeId);
        List<Document> list = documentService.getList(query, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(documentService.count(query));
        return rspData;
    }

    @Permissions("document:view")
    @GetMapping("/view.html")
    public String view(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id < 1) return error500;
        DocumentDetail document = documentService.get(id);
        if(document == null) return error500;
        String member = "普通";
        if (TKConvert.toInteger(document.getLevelId()) > 0) {
            MemberType memberType = memberTypeService.get(document.getLevelId());
            if (memberType != null) {
                member = memberType.getTitle();
            }
        }
        String documentType = "未知";
        if (document.getTypeId() != null) {
            documentType = DocumentType.getTitle(document.getTypeId());
        }
        mmap.put("document", document);
        mmap.put("member", member);
        mmap.put("documentType", documentType);
        mmap.put("company", companyService.get(document.getCompanyId()));
        mmap.put("channel", channelService.get(document.getChannelId()));
        mmap.put("subject", subjectService.get(document.getSubjectId()));
        return "document/document-view";
    }
}
